package servlet;

import entity.Admin;
import service.AdminService;
import service.Impl.AdminServiceImpl;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;

@WebServlet(name = "AdminServlet", value = "/AdminServlet")
public class AdminServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
      doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String opr = request.getParameter("opr");
        HttpSession session = request.getSession();
        if ("logout".equals(opr)){
            session.invalidate(); //session 退出失效
            response.sendRedirect("/admin/admin.jsp");
        }else if ("log".equals(opr)){
            System.out.println(opr+"----");
            String adminName = request.getParameter("adminname");
            String password = request.getParameter("password");
            AdminService adminService = new AdminServiceImpl();
            Admin admin = adminService.login(adminName,password);
            System.out.println(admin);
            if (null!=admin){ //用户不为空
                session.setAttribute("admin",admin);
                response.sendRedirect("/admin/main.jsp");
            }else {
                response.sendRedirect("/admin/admin.jsp");
            }
        }
    }

}
